package com.webcash.CNBoard.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.webcash.CNBoard.Model.Board;
import com.webcash.CNBoard.Model.Department;
import com.webcash.CNBoard.Model.DeptBoard;
import com.webcash.CNBoard.Model.DeptComment;

@Repository
public class DepartmentRepositoryImpl extends SqlSessionDaoSupport implements DepartmentRepository {

	public List<Department> selectDepartmentList() {
		SqlSession session = getSqlSession();
		
		return session.selectList("Department.selectDepartmentAllList");
	}

	public Department selectDepartmentByDeptNo(int deptNo) {
		SqlSession session = getSqlSession();
		
		return session.selectOne("Department.selectDepartmentByDeptNo", deptNo);
	}
	public List<DeptBoard> selectDepartmentBoardListByDepartmentNo(int deptNo, int pageNum) {
		SqlSession session = getSqlSession();
		
		RowBounds rowBounds= new RowBounds(10*(pageNum-1),10);
		return session.selectList("DeptBoard.selectDeptBoardListByDeptNo",deptNo, rowBounds);
	}

	public DeptBoard selectDepartmentBoardContentsByBoardNo(int deptBoardNo) {
		SqlSession session = getSqlSession();
		
		return session.selectOne("DeptBoard.selectDeptBoardByBoardNo",deptBoardNo);
	}

	public int selectDepartmentBoardNoMaxByDeptNo(int deptNo) {
		SqlSession session = getSqlSession();
		
		Map map=session.selectOne("DeptBoard.selectDepartmentBoardNoMaxByDeptNo", deptNo);
		
		if(map == null){
			return 0;
		}else{
			int deptBoardNo = (Integer) map.get("maxDeptBoardNo");
			return deptBoardNo;
		}
	}

	public void insertDepartmentBoardByDeptNo(String id, Board board, int deptNo, int deptBoardNo) {
		SqlSession session = getSqlSession();
		DeptBoard deptBoard=new DeptBoard();
		deptBoard.setDeptNo(deptNo);
		deptBoard.setDeptBoardNo(deptBoardNo);
		deptBoard.setId(id);
		deptBoard.setTitle(board.getTitle());
		deptBoard.setArticle(board.getArticle());
		deptBoard.setCommentCount(0);
		deptBoard.setReadCount(0);
		deptBoard.setWriteDate(new Date());
		deptBoard.setModifyDate(new Date());
		
		session.insert("DeptBoard.insertDeptBoardByDeptNo",deptBoard);
	}

	public int selectDepartmentBoardCountByDeptNo(int deptNo) {
		SqlSession session=getSqlSession();
		
		return session.selectOne("DeptBoard.selectDepartmentBoardCountByDeptNo",deptNo);
	}

	public DeptBoard selectDepartmentBoardByDeptNoAndDeptBoardNo(int deptNo,
			int deptBoardNo) {
		SqlSession session = getSqlSession();

		Map params = new HashMap();
		params.put("deptNo", deptNo);
		params.put("deptBoardNo", deptBoardNo);
		
		//System.out.println("DeptBoard: select deptBoard/ deptNo: "+deptNo+ "/deptBoardNo: "+deptBoardNo);
		
		return session.selectOne("DeptBoard.selectDepartmentBoardByDeptNoAndDeptBoardNo", params);
	}

	public void updateDeptBoardByDeptBoardNo(Board board, int deptBoardNo, int deptNo) {
		SqlSession session = getSqlSession();
		
		Map params = new HashMap();
		params.put("deptNo", deptNo);
		params.put("deptBoardNo", deptBoardNo);
		params.put("title", board.getTitle());
		params.put("article", board.getArticle());
		params.put("modifyDate", new Date());
		//System.out.println("department repository Update!!!!!");
		
		session.update("DeptBoard.updateDeptBoardByDeptBoardNo",params);
	}

	public void deleteDeptBoardByDeptNoAndDeptBoardNo(int deptNo, int deptBoardNo, int boardNo) {
		SqlSession session = getSqlSession();
		Map params = new HashMap();
		params.put("deptNo", deptNo);
		params.put("deptBoardNo", deptBoardNo);
		
		
		session.delete("DeptBoard.deleteDeptBoardByDeptNoAndDeptBoardNo",params);
	}

	public void updateDeptBoardForReadCountByDeptBoardNo(int deptNo, int deptBoardNo,
			int readCount) {
		SqlSession session = getSqlSession();
		Map params = new HashMap();
		params.put("deptNo", deptNo);
		params.put("deptBoardNo", deptBoardNo);
		params.put("readCount", readCount);
		
		session.delete("DeptBoard.updateDeptBoardForReadCountByDeptBoardNo", params);
	}

	public void insertDeptCommentByBoardNoAndId(int boardNo, String id,
			String commentContents) {
		SqlSession session = getSqlSession();
		
		DeptComment deptComment = new DeptComment();
		deptComment.setBoardNo(boardNo);
		deptComment.setId(id);
		deptComment.setCommentLevel(0);
		deptComment.setWriteDate(new Date());
		deptComment.setCommentContents(commentContents);
		
		session.insert("DeptBoard.insertDeptCommentByBoardNoAndId", deptComment);
	}

	public void updateDeptBoardForCommentCountByBoardNo(int boardNo,
			int commentCount) {
		SqlSession session = getSqlSession();
		
		Map params = new HashMap();
		params.put("boardNo", boardNo);
		params.put("commentCount", commentCount);
		session.update("DeptBoard.updateDeptBoardForCommentCountByBoardNo", params);
	}

	public List<DeptComment> selectDeptCommentListByBoardNo(int boardNo) {
		SqlSession session = getSqlSession();
		
		return session.selectList("DeptBoard.selectDeptCommentListByBoardNo", boardNo);
	}

	public void insertDepartment(Department dept) {
		SqlSession session = getSqlSession();
		session.insert("Department.insertDepartment", dept);
	}

	
	
}
